<template>
    <div class="wrapper">
        <div class="header">
            <el-row style="margin-bottom:20px">
                <el-button type="danger" style="width:120px" @click="centerDialogVisible=true;title='新增'">新建</el-button>
            </el-row>
             <el-row>
                <el-col :span="4" style="margin-right:30px">
                    <el-input v-model="searchBo.name" placeholder="指令簇名称"></el-input>
                </el-col> <el-button type="danger" style="width:120px" @click="getList">查询</el-button>
            </el-row>
        </div>
        <div class="body">
            <Table 
                :list="list" 
                :state="false" 
                v-loading="loading" 
                :total="total" 
                @sizeChange="handSize"
                @currentChange='currentChange'
                >
                <slot>
                     <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                @click="handleEdit(scope.row)">修改
                            </el-button>
                            <el-button
                                size="mini"
                                type="danger"
                                @click="handleDel(scope.row)">删除
                            </el-button>
                        </template>
                    </el-table-column>
                </slot>
            </Table>
        </div>
        <el-dialog
            :title="title"
            :visible.sync="centerDialogVisible"
             :before-close="onCancel"
            width="30%"
            >
            <div>
                <el-form ref="addForm" :model="addForm" :rules="rule" label-position="right" label-width="140px">
                    <el-form-item label="指令簇名称" prop='name'>
                        <el-input v-model="addForm.name" placeholder="请输入指令簇名称"></el-input>
                    </el-form-item>
                    <el-form-item label="指令簇编码" prop='value'>
                        <el-input v-model="addForm.value" placeholder="请输入指令簇编码"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop='remark'>
                        <el-input v-model="addForm.remark" placeholder="请输入备注"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="onCancel">取 消</el-button>
                <el-button type="primary" @click="onOk">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {directiveGroupList,directiveGroupAdd,
directiveGroupEdit,directiveGroupDel

} from '../../../api/facility'
import Table from '@/components/Table'
export default {
    components:{
        Table
    },
    data () {
        return {
            list:{
                thead:[
                    // {label:'编号',prop:"index",rowspan:1},
                    {label:'指令簇名称',prop:"name",rowspan:1},
                    {label:'指令簇编码',prop:"value",rowspan:1},
                    {label:'备注',prop:"remark",rowspan:1},
                ],
                tbody:[]
            },
            loading:false,
            total:0,
            title:'新增',
            centerDialogVisible:false,
            addForm:{
                name:'',
                value:'',
                remark:''
            },
            rule:{
                name:[  { required: true, message: '请输入指令簇名称', trigger: 'blur' },],
                value:[  { required: true, message: '请输入指令簇编码', trigger: 'blur' },],
                remark:[  { required: true, message: '请输入备注', trigger: 'blur' },]
            },
            pageSize:20,
            pageNum:1,
            total:0,
             searchBo:{}

        }
        
    },
    mounted () {
        this.getList()
    },
    methods:{
        onOk () {
            this.$refs['addForm'].validate((vaild)=>{
                if(vaild) {
                    if(this.title == '新增'){
                        directiveGroupAdd(this.addForm).then(res=>{
                            if(res.status == 200) {
                                this.$message({
                                    type:'success',
                                    message:"新增成功！"
                                })
                                this.getList()
                                this.onCancel()
                            }else{
                                this.$message.error(res.msg)
                            }
                        })
                    }else{
                         directiveGroupEdit(this.addForm).then(res=>{
                            if(res.status == 200) {
                                this.$message({
                                    type:'success',
                                    message:"修改成功！"
                                })
                                this.getList()
                                this.onCancel()
                            }else{
                                this.$message.error(res.msg)
                            }
                        })
                    }
                }
            })
           
        },
        onCancel () {
            this.centerDialogVisible = false;
            this.addForm=this.$options.data().addForm
            this.$refs['addForm'].clearValidate();
            this.$refs['addForm'].resetFields();
        },
        getList () {
            this.loading = true;
            let obj = {
                pageNum:this.pageNum,
                pageSize:this.pageSize,
                ...this.searchBo
            }
            directiveGroupList(obj).then(res=>{
                if(res.status == 200) {
                    this.total = res.data.total;
                    this.list.tbody = res.data.list;
                }
            }).finally(res=>{
                this.loading =false
            })
        },
        handSize (val) {
            this.pageNum = val;
            this.getList()
        },
        currentChange (val) {
            this.pageSize = val;
            this.getList()
        },
        handleEdit (row) {
            this.title = '修改'
            this.addForm = {...row};
            this.centerDialogVisible = true;
        },
        handleDel (row) {
            console.log(row)
             this.$confirm('此操作将永久删除项, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    directiveGroupDel(row).then(res=>{
                        if(res.data) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.getList()
                        }else{
                            this.$message({
                                type:'error',
                                message:res.msg
                            })
                        }
                    })
                    
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        }
    }
}
</script>

<style lang="less" scoped>
    .wrapper{
        width: 100%;
         overflow: auto;
        .header{
            height: 190px;
            margin-bottom: 25px;
            background-color: #fff;
            padding:38px 20px;
            box-sizing: border-box;;
        }
        .body{
            width: 100%;
            height:500px ;
            box-sizing: border-box;
            padding:10px 20px;
            overflow: auto;
            background-color: #fff;
        }
        

    }
</style>